<template>
  <el-row>
    <el-col :sm="{span:10,offset:5}">
      <el-form :model="linkInfo" ref="ruleForm" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="linkInfo.name" type="text">
          </el-input>
        </el-form-item>
        <el-form-item label="去向链接" prop="toLink">
          <el-input v-model="linkInfo.toLink" type="text">
          </el-input>
        </el-form-item>
        <el-form-item label="描述信息" prop="description">
          <el-input v-model="linkInfo.description" type="text">
          </el-input>
        </el-form-item>
        <el-form-item label="图标链接" prop="imgLink">
          <el-input v-model="linkInfo.imgLink" type="text">
          </el-input>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button @click="commit('ruleForm')" type="success">提交</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import axios from "axios"
export default {
  name: "addLink",
  data(){
    return{
      linkInfo: {
        name: '',
        toLink: '',
        imgLink: '',
        description: '',
        md5Password: ''
      },
      rules:{
        name: [{required:true,message:"名称不能为空",trigger:'blur'}],
        toLink: [{required:true,message:'去向链接不能为空',trigger:'blur'}],
        imgLink: [{required:true,message:'图片链接不能为空',trigger:'blur'}],
        description: {required:true,message:'描述信息不能为空',trigger:'blur'}
      }
    }
  },
  mounted(){
    let id = this.$route.query.id;
    if ( id != undefined){
      axios.get("/admin/selectOneLink",{params:{id:id}}).then( (res) => {
        this.linkInfo = res.data;
      })
    }
  },
  methods: {
    commit(ruleForm){
      this.$refs[ruleForm].validate( (valid) => {
        if ( valid ){
          this.linkInfo.md5Password = localStorage.getItem("password");
          if ( this.$route.query.id === undefined){
            axios.post("/admin/insertFriendLink",this.linkInfo).then( (res) => {
              if ( res.data === true){
                this.$message.success("添加成功");
                this.$router.push({
                  name: 'friendLinks'
                })
              }
              else
                this.$message.error("添加失败");
            })
          }
          else {
            axios.post("/admin/updateFriendLink",this.linkInfo).then( (res) => {
              if ( res.data === true){
                this.$message.success("修改成功");
                this.$router.push({
                  name: 'friendLinks'
                })
              }
              else
                this.$message.error("修改失败");
            })
          }

        }else {

        }
      })
    }
  }
}
</script>

<style scoped>

</style>